package com.kude.service;

import java.util.HashMap;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.kude.entity.Article;
import com.kude.entity.ArticleCategory;
import com.kude.entity.Product;
import com.kude.entity.ProductCategory;
import com.kude.manager.ArticleManager;
import com.kude.manager.OperException;
import com.kude.manager.ProductManager;
import com.kude.util.BeanFactory;

public class ProductService {
private ProductManager productManager;
	
	/**
	 * 根据ID获取产品
	 * 
	 * @param id
	 * @return
	 * @throws OperException
	 */
	public Product getProduct(int id) throws OperException {
		return productManager.get(id);
	}
	
	/**
	 * 更新产品
	 * 
	 * @param product
	 * @throws OperException
	 */
	public void updateProduct(Product product) throws OperException {
		productManager.update(product);
	}
	
	/**
	 * 根据ID删除产品
	 * @param id
	 */
	public void deleteProduct(int id) throws OperException{
		productManager.delete(id);
	}
	/**
	 * 添加产品
	 * @param product
	 * @throws OperException
	 */
	public void addProduct(Product product) throws OperException{
		productManager.add(product);
	}
	/**
	 * 根据类别ID获取产品列表并分页
	 * @param pageNow
	 * @param pageSize
	 * @param categoryId
	 * @throws OperException 
	 */
	public List<Product> getProductListByCategory(int pageNow, int pageSize,int categoryId) throws OperException{
		if(pageNow <= 0 && pageSize <= 0){
			return null;
		}
		int offset = (pageNow - 1) * pageSize;
		int limit = pageSize;
		SimpleExpression res = Restrictions.eq("productCategory.categoryId", categoryId);
		Order order = Order.desc("time");
		return productManager.find(order, offset, limit, res);
	}
	
	/**
	 * 根据类别ID集合获取产品列表并分页，用于显示所属较大类别的筛选
	 * @param pageNow 
	 * @param pageSize 
	 * @param List<ProductCategory>
	 * @throws OperException 
	 */
	public List<Product> getProductListByCategoryIdList(int pageNow, int pageSize,List<ProductCategory> productCategory) throws OperException{
		if(pageNow <= 0 && pageSize <= 0){
			return null;
		}
		if(productCategory.size() == 0 ){
			return null;
		}
		int offset = (pageNow - 1) * pageSize;
		int limit = pageSize;
		HashMap<String, Integer> categoryMap = new HashMap();
		for(int i = 0;i<productCategory.size();i++){
			categoryMap.put("productCategory.categoryId", productCategory.get(i).getCategoryId());
		}
		Order order = Order.desc("time");
		return productManager.find(order, offset, limit, Restrictions.allEq(categoryMap));
	}
	
	/**
	 * 获取新品或者旧品的产品
	 * @param pageNow
	 * @param pageSize
	 * @param isNew 
	 * @return
	 * @throws OperException
	 */
	public List<Product> getProductByIsNew(int pageNow, int pageSize,int isNew) throws OperException{
		if(pageNow <= 0 && pageSize <= 0){
			return null;
		}
		int offset = (pageNow - 1) * pageSize;
		int limit = pageSize;
		SimpleExpression res = Restrictions.eq("isNew", isNew);
		Order order = Order.desc("time");
		return productManager.find(order, offset, limit, res);
	}
	
	/**
	 * 获取畅销品或非畅销品
	 * @param pageNow
	 * @param pageSize
	 * @param isOnsale 
	 * @return
	 * @throws OperException
	 */
	public List<Product> getProductByIsOnsale(int pageNow, int pageSize,int isOnsale) throws OperException{
		if(pageNow <= 0 && pageSize <= 0){
			return null;
		}
		int offset = (pageNow - 1) * pageSize;
		int limit = pageSize;
		Order order = Order.desc("time");
		System.out.println(productManager);
		return productManager.find(order, offset, limit, Restrictions.eq("isNew", isOnsale));
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	
	

	
}
